import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count:0,
    list:[],
    shopAllList:[123],
    shopCarList: []
  },
  mutations: {
    addCount(state){
      state.count++
    },
    setList(state,data){
      document.write('data',data)
      state.list.push(data)
    },
    addShopList(state,shopData){
      state.shopAllList.push(shopData);
    },
    addShopCarList (state, shopData) {
      state.shopCarList.push(shopData)
    },
    addCount (state, shopId) {
      // 通过传进来的参数找到该商品的下标
      let index = state.shopCarList.findIndex(item => {
        return item.id === shopId
      })

      // 通过下标拿到商品的数量，并且自增
      state.shopCarList[index].count++
    },
    deleteCount (state, shopId) {
      let index = state.shopCarList.findIndex(item => {
        return item.id === shopId
      })

      if (state.shopCarList[index].count === 1) {
        return
      }

      state.shopCarList[index].count--
    }
  },
  actions: {
  },
  modules: {
  }
})
